<?
header('Content-type: text/xml');
include_once (dirname(__FILE__) . '/libs/database/db.php');
?>
<data>
<?


function get_prj_color($prj_id, $color) {
	if (trim($color) == "") {
		$sql = "SELECT" .
		" * FROM " . TB_CONFIG . " WHERE PRJ_ID='$prj_id'";

		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		$color = $row['COLOR'];
	}

	if (trim($color) == "")
		return "";
	else
		return "color=\"$color\"";
}

function get_prj_icon($prj_id, $icon) {
	if (trim($icon) == "") {
		$sql = "SELECT" .
		" * FROM " . TB_CONFIG . " WHERE PRJ_ID='$prj_id'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		$icon = $row['ICON'];
	}

	if (trim($icon) == "")
		return "";
	else {
		if (strlen($icon) >= 4 && strtolower(substr(trim($icon), 0, 4)) == "http") {
			// NOOP
		} else {
			$icon = "images/icons/$icon";
		}

		return "icon=\"$icon\"";
	}
}

function get_prj_tape_image($prj_id, $tapeImage) {
	if (trim($tapeImage) == "") {
		$sql = "SELECT" .
		" * FROM " . TB_CONFIG . " WHERE PRJ_ID='$prj_id'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		$tapeImage = $row['TAPE_IMAGE'];
	}

	if (trim($tapeImage) == "")
		return "";
	else {
		if (strlen($tapeImage) >= 4 && strtolower(substr(0, 4)) == "http") {
			// NOOP
		} else {
			$tapeImage = "images/tapes/$tapeImage";
		}

		return "tapeImage=\"$tapeImage\"";
	}
}

function get_prj_title_link($prj_id, $title_link) {
	if (trim($title_link) == "") {
		$sql = "SELECT" .
		" * FROM " . TB_CONFIG . " WHERE PRJ_ID='$prj_id'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		$title_link = $row['TITLE_LINK'];
	}

	if (trim($title_link) == "")
		return "";
	else
		return "link=\"$title_link\"";
}

$sql = "SELECT " .
" ID," .
" START," .
" END," .
" UNIX_TIMESTAMP(START) AS U_START," .
" UNIX_TIMESTAMP(END) AS U_END," .
" TO_DAYS(END) - TO_DAYS(START) AS DURATION," .
" TO_DAYS(END) - TO_DAYS(NOW()) AS DUE_END," .
" TO_DAYS(START) - TO_DAYS(NOW()) AS DUE_START," .
" WEEK(START) AS W_START," .
" WEEK(END) AS W_END," .
" IMAGE," .
" TITLE," .
" COLOR," .
" TAPE_IMAGE," .
" ICON," .
" IS_DURATION," .
" COMPLETE," .
" PAGE_URL," .
" TITLE_LINK," .
" PRIORITY," .
" OWNER," .
" PRJ_ID," .
" DESCRIPTION" .
" FROM " . TB_EVENT;
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
	$id = $row['ID'];
	$start = gmdate("M d Y  H:h:s", $row['U_START']);
	$w_start = $row['W_START'] + 1;
	$w_end = $row['W_END'] + 1;
	$image = $row['IMAGE'];
	$title = $row['TITLE'];
	$is_duration = $row['IS_DURATION'];
	$duration = $row['DURATION'];
	$prj_id = $row['PRJ_ID'];
	$color = get_prj_color($prj_id, $row['COLOR']);
	$icon = get_prj_icon($prj_id, $row['ICON']);
	$tape_image = get_prj_tape_image($prj_id, $row['TAPE_IMAGE']);
	$owner = $row['OWNER'];
	$priority = $row['PRIORITY'];
	$complete = $row['COMPLETE'];
	$desc = $row['DESCRIPTION'];
	$page_url = $row['PAGE_URL'];
	$title_link = get_prj_title_link($prj_id, $row['TITLE_LINK']);
	$desc .= "<br />";
	if (trim($page_url) != "")
		$desc .= "<br /><b>Page</b>: <a href='$page_url' target='_blank'>$page_url</a>";

	if (trim($owner) != "")
		$desc .= "<br /><b>Owner</b>: $owner";

	if ($complete != 0)
		$desc .= "<br /><b>Complete</b>: $complete%";

	if ($priority != 0)
		$desc .= "<br /><b>Priority</b>: $priority(1-10)";

	$end_time = "";
	if ($is_duration == "true") {
		$end = gmdate("M d Y  H:h:s", $row['U_END']);

		if (trim($row['END']) != "")
			$end_time = "end=\"$end GMT\"";

		$desc .= "<br /><b>Duration</b>: $duration days(WW$w_start - WW$w_end)";
		$desc .= "<br /><b>Time Remaining</b>: " . $row['DUE_END'] . " days";
	} else {
		$desc .= "<br /><b>Time</b>: WW$w_start";
		$desc .= "<br /><b>Time Remaining</b>: " . $row['DUE_START'] . " days";
	}

	$desc .= " <input type='hidden' value='$prj_id'>";

	$desc .= "<br /><button id='show$id' onclick=\"getProjectInfo($id);\">Edit</button>";
	$desc .= "<button id='del$id' onclick=\"delProject($id);\">Del</button>";
	$desc = htmlspecialchars($desc, ENT_QUOTES);
?>
<event start="<?=$start?> GMT" <?=$end_time?> 
	image="<?=$image?>" 
	title="<?=$title?>" 
	isDuration="<?=$is_duration?>" 
	<?=$color?>  
	<?=$tape_image?>
	<?=$title_link?>  
	<?=$icon?> 
	><?=$desc?></event>
<?


}
?>  
</data>